package cn.edu.zjgsu.ito.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.edu.zjgsu.ito.model.StudentModel;

import com.jfinal.kit.EncryptionKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class StuInfService extends Service {

	/*
	 * 张虎涛
	 * 2016/06/01
	 */
	public Record search(String passportNo) throws ParseException {
		java.text.SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String []b= new String[100];
		Date []date=new Date[100];
		Date dates=new Date();
		String Dates="";
		Record studentInfo = Db.findFirst("select * from student_info where passportNo=?",passportNo);
		List<Record> studentnewpassportNo = Db.find("select * from newpassportno where passportNo=?",passportNo);			
		if(studentnewpassportNo.size()>1){
			for(int j=0;j<studentnewpassportNo.size();j++){
								b[j] = studentnewpassportNo.get(j).getStr("time");
								date[j] = formatter.parse(b[j]);
							}
							System.out.println(studentnewpassportNo.size());
							for(int a=0;a<studentnewpassportNo.size()-1;a++){
								 dates = date[a];
								int c = (date[a+1]).compareTo(dates);						
								if(c>0){
									dates = date[a+1];
								}
								
							}								
							Dates = formatter.format(dates);
							System.out.println(Dates);
							Record newpassportNo = Db.findFirst("select * from newpassportno where time=?",Dates);
							System.out.println(newpassportNo.getStr("newpassportNo"));
							studentInfo.set("newpassportNo",newpassportNo.getStr("newpassportNo"));
			}
		else{
			System.out.println("[][][][][][][][][][]");
			Record newpassportNo = Db.findFirst("select * from newpassportno where passportNo=?",passportNo);
			System.out.println("111111111");
			System.out.println(newpassportNo.getStr("newpassportNo"));
			studentInfo.set("newpassportNo",newpassportNo.getStr("newpassportNo"));
		}		
			return studentInfo;
		}
//		try {
//			setStrSql(StudentModel.dao.selectStuInfoSql);
//
//			setRecord(Db.findFirst(getStrSql(), passportNo));
//			return getRecord();
//		} catch (Exception e) {
//			e.printStackTrace();
//			return null;
//		}


	public boolean checkPassword(String name, String oldPassword) {

		oldPassword = EncryptionKit.md5Encrypt(oldPassword);// 加密

		boolean tf = false;

		try {
			setStrSql(StudentModel.dao.checkStuPasSql);

			setRecord(Db.findFirst(getStrSql(),name));

			tf = oldPassword.equals(getRecord().getStr("sPwd"));

			return tf;
		} catch (Exception e) {
			e.printStackTrace();
			return tf;
		}

	}

	public boolean updatePassword( String newPassword,String name) {

		newPassword = EncryptionKit.md5Encrypt(newPassword);// 加密

		try {
			setStrSql(StudentModel.dao.updateStuPasSql);

			Db.update(getStrSql(), newPassword, name);

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}
	
	public boolean updateStuInf( String newPhone,String newEmail,String name) {

		try {
			setStrSql(StudentModel.dao.updateStuInfoSql);

			Db.update(getStrSql(), newPhone,newEmail, name);

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}
    
    public boolean updateStuInfPhone(String phone,String passportNo) {

		try {		

			Db.update("update student_info set phone=? where passportNo=?", phone,passportNo);

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}
    public boolean updateStuInfMail(String mailbox,String passportNo) {

		try {		

			Db.update("update student_info set mailbox=? where passportNo=?", mailbox,passportNo);

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}


}
